import Vue from 'vue';
import VueResource from 'vue-resource';
import global_ from '../router/global';
import qs from 'qs'

import { baseUrl, pcUrl } from '../config/env'

Vue.use(VueResource);


// HTTP相关
Vue.http.options.emulateJSON = true;
Vue.http.options.emulateHTTP = true;
Vue.http.options.crossOrigin = true;
Vue.http.options.withCredentials = true;

Vue.http.options.headers = {
    'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
};

Vue.prototype.global = global_;
Vue.http.options.params = {
    'r_key': Vue.prototype.global.r_key,
    'p_key': Vue.prototype.global.p_key
};

//console.log(Vue.http)
//console.log(Vue.http);
// 返回一个Promise
export function fetch(url, params = {}, type = "GET", point = 'mobile') {
    type = type.toUpperCase();

    if (point == 'mobile' )
        url = baseUrl + url;
    else
        url = pcUrl + url;



    //console.log(type);

    return new Promise((resolve, reject) => {
        if (type == 'GET') {
            
            console.log('get')

            var val = '';
            
            if (typeof (params) == 'object' && JSON.stringify(params) != "{}") val = qs.stringify(params);
            if(val!='') url = url + '&'+ val;
            Vue.http.get(url)
                .then(response => {
                    resolve(response.data);
                }, err => {
                    reject(err);
                })
                .catch((error) => {
                    reject(error)
                })
        } else {
            

            Vue.http.post(url, params)
                .then(response => {
                    resolve(response.data);
                }, err => {
                    reject(err);
                })
                .catch((error) => {
                    reject(error)
                })
        }

    })
}


//api.p2p.com / index.php ? s = /Home/Users / c
export default {
    com: {

        //首页信息
        index(){
            return fetch('/Users/indexList.html');
        }
    },
    about: {

        //获取所有合作伙伴列表
        get_partner_list(params) {
            return fetch('/Article/getpartner.html' ,params)
        },

        //获取合作伙伴详情
        get_partner_detail(params) {
            return fetch('/Article/partner_detail.html', params)
        }
    },

    user: {
        
        //验证旧手机验证码
        send_sms_by_verify_original_phone(params) {
            return fetch('/Users/old_phone_sms.html', params, 'POST')
        },

        //验证原来手机
        verify_original_phone(params) {
            return fetch('/Users/confirm_old_phone.html', params, 'POST')
        },

        //修改手机的验证码
        send_sms_by_verify_new_phone(params) {
            return fetch('/Users/new_phone_sms.html', params, 'POST')
        },

        //修改手机
        update_phone(params) {
            return fetch('/Users/user_edit_phone.html', params, 'POST')
        },


        //图形验证码
        verifyImg() {
            return baseUrl + '/Users/verify.html';
        },

        //个人信息
        profile(){
            return fetch('/Users/usersInfo.html');
        },

        //修改头像的密钥
        get_protrait_key() {
            return fetch('/Users/getKey.html');
        },

        //修改头像
        update_protrait(params){
            //return fetch('?user&q=api/setuser/headUpload', params, 'POST', 'pc');
            return fetch('?user&q=api/setuser/headUpload', params, 'POST', 'pc');
        }
    }
}
